package com.baomidou.springwind.service.impl;

import com.baomidou.kisso.common.encrypt.SaltEncoder;
import com.baomidou.springwind.entity.UserRole;
import com.baomidou.springwind.service.IUserRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.framework.annotations.Log;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.springwind.entity.User;
import com.baomidou.springwind.mapper.UserMapper;
import com.baomidou.springwind.service.IRoleService;
import com.baomidou.springwind.service.IUserService;
import com.baomidou.springwind.service.support.BaseServiceImpl;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

/**
 *
 * User 表数据服务层接口实现类
 *
 */
@Service
public class UserServiceImpl extends BaseServiceImpl<UserMapper, User> implements IUserService {

	@Autowired
	private IRoleService roleService;

	@Autowired
	private IUserRoleService userRoleService;

	@Log("登录")
	@Override
	public User selectByLoginName(String loginName) {
		User user = new User();
		user.setLoginName(loginName);
		return super.selectOne(new EntityWrapper<User>(user));
	}

	@Log("删除用户")
	@Override
	public void deleteUser(Long userId) {
		// 删除用户角色，再删除用户
		roleService.deleteByUserId(userId);
		super.deleteById(userId);
	}

	@Log("添加逻辑用户")
	@Transactional(rollbackFor = Exception.class)
	@Override public boolean insertUserLogic(User user, Long rid)
	{
		boolean rlt = false;
		if ( user != null ) {
			user.setPassword(SaltEncoder.md5SaltEncode(user.getLoginName(), user.getPassword()));
			if ( user.getId() != null ) {
				EntityWrapper ew = new EntityWrapper(new UserRole());
				ew.where("uid={0}", rid);
				UserRole userRole = userRoleService.selectOne(ew);
				userRoleService.updateById(userRole);
				rlt = updateById(user);
			} else {
				user.setCrTime(new Date());
				user.setLastTime(user.getCrTime());
				UserRole userRole = new UserRole();
				userRole.setRid(rid);
				insert(user);
				userRole.setUid(user.getId());
				rlt = userRoleService.insert(userRole);
			}
		}
		return rlt;
	}

	@Log("添加用户")
	public boolean insert(User entity) {
		System.err.println(" 覆盖父类方法 ");
		return super.insert(entity);
	}
}